package 验证二叉搜索树;

import java.util.Stack;

public class Solution2 {
    public boolean isValidBST(TreeNode root) {
        // 迭代法，采用中序遍历
        Stack<TreeNode> stack = new Stack<>();
        TreeNode p = root;
        TreeNode pre = null;
        while (p != null || !stack.isEmpty()) {
            if (p != null) {
                // 左
                stack.push(p);
                p = p.left;
            } else {
                // 根
                p = stack.pop();
                if (pre == null || pre.val < p.val) {
                    pre = p;
                }else{
                    return false;
                }
                // 右
                p = p.right;
            }
        }
        return true;
    }
}
